package com.hua.controller;

import com.hua.entity.Msm;
import com.hua.entity.SysUserRoleEntity;
import com.hua.entity.User;
import com.hua.service.SysUserRoleService;
import com.hua.service.UserService;
import com.hua.utils.RandomUtil;
import com.hua.utils.Result;
import com.hua.utils.SystemConstants;
import com.hua.utils.sendPhone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/api/msm")
public class MsmController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private sendPhone sendPhone;

    @Autowired
    private UserService userService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SysUserRoleService sysUserRoleService;

//    @Autowired
//    private SysUserRoleEntity sysUserRoleEntity;




    //发送短信的方法
    @PostMapping("/send")
    public Result sendMsm(@RequestBody Msm msm) {

        System.out.println(msm);



        //1,从redis获取验证码，如果获取到直接返回
//        String code = redisTemplate.opsForValue().get(phone);
//        if (!StringUtils.isEmpty(code)) {
//            return Result.ok();
//        }

        // 生成随机值，传递给阿里云发送
        String code = RandomUtil.getFourBitRandom();
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        //调用service发送短信的方法
        boolean isSend = sendPhone.send(param, msm.getPhone());
        if (isSend) {
            //发送成功，把发送成功的验证码放到redis里面
            //设置有效时间
            redisTemplate.opsForValue().set(msm.getPhone(), code, 5, TimeUnit.MINUTES);
            System.out.println(code);
            return Result.ok(code).message("短信发送成功");
        } else {
            return Result.Error().message("短信发送失败");
        }
    }


    /**
     *  添加用户
     * @param user
     * @return
     */
    @PostMapping("/add")
//    @PreAuthorize("hasAuthority('sys:user:add')")
    public Result add(@RequestBody User user) {

        //1,从redis获取验证码，如果获取到直接返回
//        String code = redisTemplate.opsForValue().get(user.getPhone());
//        if (!StringUtils.isEmpty(code)) {
//            return Result.ok();
//        }


        user.setDepartmentId(3L);
        //查询用户
        User item = userService.findUserByUserName(user.getUsername());
        user.setNickName(user.getRealName());
        user.setDepartmentName("人事管理部");



        //判断对象是否为空
        if (item != null) {
            return Result.Error().message("该登录名称已被使用，请重新输入！");
        }
        //如果没有上传头像
        if(ObjectUtils.isEmpty(user.getAvatar())){
            user.setAvatar(SystemConstants.DEFAULT_AVATAR);
        }
        //密码加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //调用保存用户信息的方法
        if(userService.save(user)){
            SysUserRoleEntity userRole=new SysUserRoleEntity();
            userRole.setUser_id(user.getId());
            userRole.setRole_id(2L);
            System.out.println(userRole);
            boolean aa = sysUserRoleService.save(userRole);
            System.out.println(aa);
            return Result.ok().message("用户添加成功");
        }
        return Result.Error().message("用户添加失败");
    }
}
